home *** CD-ROM | disk | FTP | other *** search
/ Whiteline: delta / whiteline CD Series - delta.iso / tools / utils / twtcp122 / pktdrv / pktdlink / cookie.c next >
C/C++ Source or Header  |  1995-11-25  |  2KB  |  92 lines

  1. /********************************************************************/
  2. /*                                                                    */
  3. /*    Packet driver for AMD LANCE ethernet controller                    */
  4. /*                                                                    */
  5. /*    Copyleft by H. Wieser, 1992 TU-Vienna IAEE                        */
  6. /*    All rights reserved                                                */
  7. /*                                                                    */
  8. /********************************************************************/
  9.  
  10.  
  11.  
  12. #include <stdlib.h>
  13. #include <tos.h>
  14. #include "cookie.h"
  15. #include <stdio.h>
  16.  
  17. #ifndef NULL
  18. #define NULL (void *)0L
  19. #endif
  20.  
  21. #define _cookiejar    *(long *)0x5A0L
  22.  
  23.  
  24. COOKIE *new_cookiejar(long n)
  25. {
  26.   register long superstack;
  27.   register COOKIE *jar,*newjar;
  28.   register long i,c;
  29.  
  30.   jar = get_cookiejar();
  31.   i=0;
  32.   if(jar) for(; jar[i].id != ENDCOOKIE; i++);  /* get size of old jar */
  33.   if(jar && jar[i].val > n) return(jar);  /* there isroom in jar */
  34.  
  35.   newjar = (COOKIE *)malloc(n*sizeof(COOKIE));/* alloc new jar */
  36.   if(jar && newjar) for(c=0; c<i; c++)
  37.   {  /* copy old jar */
  38.     newjar[c].id = jar[c].id;
  39.     newjar[c].val = jar[c].val;
  40.   }
  41.   newjar[i].id = ENDCOOKIE;  /* terminate new jar */
  42.   newjar[i].val = n;
  43.   superstack = Super((void *)0);
  44.   _cookiejar = (long)newjar;  /* install new jar */
  45.   Super((void*)superstack);
  46.   return(newjar);
  47. }
  48.  
  49. COOKIE *get_cookiejar(void)
  50. {
  51.   register long superstack;
  52.   register long cookieaddress;
  53.   
  54.   if(Super((void*)1L))
  55.   {
  56.     cookieaddress = _cookiejar;  /* return address of cookiejar */
  57.   }
  58.   else
  59.   {
  60.     superstack = Super(0L);
  61.     cookieaddress = _cookiejar;  /* return address of cookiejar */
  62.     Super((void*)superstack);
  63.   }
  64.     return (COOKIE *)cookieaddress;
  65. }
  66.  
  67. COOKIE *add_cookie(long id,long val)
  68. {
  69.   register COOKIE *jar;
  70.   if((jar = new_cookiejar(8L))==NULL) return(NULL);
  71.   while(jar->id != ENDCOOKIE) jar++;  /* search end of cookiejar */
  72.   jar->id = id;
  73.   (jar+1)->val = jar->val;  /* keep size of jar */
  74.   jar->val = val;
  75.   (jar+1)->id = ENDCOOKIE;
  76.   return(jar);
  77. }
  78.  
  79.  
  80. COOKIE *get_cookie(long id)
  81. {
  82.   register COOKIE *jar;
  83.   if((jar = get_cookiejar()) == NULL) return(NULL);
  84.   while(jar->id)
  85.   {
  86.     if(jar->id == id) return(jar);  /* find cookie */
  87.     jar++;
  88.   }
  89.   return(NULL);
  90. }
  91.  
  92.